<?php
/*!
 * LostKids PHP Library v0.1 
 * http://huali.li/
 *
 * Copyright 2011, 
 * Written By James Pang
 *  
 * Date: 2011/2/14
 */

if (!session_id()) {
    session_start();
}

require_once ('weibooauth.php');

class WeiboClientXAuthed {
    public $wbClient;

    public function __construct() {
        if (!isset($_SESSION['last_key'])) {
            $o = new WeiboOAuth(WB_AKEY, WB_SKEY);
            $parameters = array(
                'x_auth_username' => x_auth_user,
                'x_auth_password' => x_auth_pwd,
                'x_auth_mode' => 'client_auth'
            );
            $request = $o->oAuthRequest($o->accessTokenURL(), 'POST', $parameters);
            $last_key = OAuthUtil::parse_parameters($request);

            if(!isset($last_key['oauth_token'])) {
                $msg = 'Fail Xauth! Sina='.WB_AKEY;
                error_log($msg);
            	echo $msg;
                die();
            }

            $o->token = new OAuthConsumer($last_key['oauth_token'], @$last_key['oauth_token_secret']);
            $_SESSION['last_key'] = $last_key;
        }
        
        $this->wbClient = new WeiboClient(WB_AKEY, WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
    }
}
?>